package com.example.ruihuada.ui.HomeModule.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.ruihuada.R
import com.example.ruihuada.widget.webEditor.*
import kotlinx.android.synthetic.main.activity_second_kill.*
import org.json.JSONObject

class SecondKillActivity : AppCompatActivity(),
    OnJsEditorStateChangedListener {

    enum class EditorState {
        PUBLISH,

        //判断是否为文本内容改变的事件
        SELECT,

        //判断是否是返回键的事件
        FINISH,
        PREVIEW,
        NONE
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second_kill)


        val htmlEditor: String = Utils.getHtmlFromFile(this, "editor/editor.html")
        val editorConfig = EditorConfig.Builder().baseUrl("file:///android_asset/editor/")
            .htmlEditor(htmlEditor)
            .titleHint("#请在此处输入文本内容")
            .contentHint("开始您的创作吧(请输入80个字以上)")
            .oldTitle("#请在此处输入文本内容")
            .oldHtml("#请在此处输入文本内容")
            .build()

       var aiwoEditor = MyEditor()
        aiwoEditor?.initialize(editorWebView, editorConfig, this)
    }

    override fun onLinkTapped(url: String?, title: String?) {

    }

    override fun onGetHtmlResponse(inputArgs: MutableMap<String, String>) {
        val functionId: String? = inputArgs["function"]
        if (functionId.isNullOrEmpty()) {
            return
        }

        when (functionId) {
            "getAllHTMLForCallback" -> {
                val title: String? = inputArgs["title"]
                val content: String? = inputArgs["content"]
                val images: String? = inputArgs["images"]
                val editorStatus: Int = (inputArgs["editorStatus"] ?: "0").toInt()
                this.title = title
//                this.content = content
//                runOnUiThread {
//                    when (editorState) {
//                        EditorState.PUBLISH -> handlePublish(images, editorStatus)
//                        EditorState.FINISH -> handleFinish(images, editorStatus)
//                        EditorState.SELECT -> handlePublishEnable(images, editorStatus)
//                        EditorState.PREVIEW -> EggArticleActivity.start(this, content)
//                        EditorState.NONE -> {
//                        }
//                    }
//                }
            }
        }
    }

    override fun onSelectionChanged(selectionArgs: MutableMap<String, String>) {
        val mFocusedFieldId = selectionArgs["id"]
        if (mFocusedFieldId != null && mFocusedFieldId.isNotEmpty()) {
            runOnUiThread {
//                editorState = EditorState.SELECT
//                aiwoEditor?.getAllHTML()
            }

        }
    }

    override fun onFocusIn(focusId: String?) {
        if (focusId == null) {
            return
        }
        runOnUiThread {
            when (focusId) {
                "zss_field_title" -> {
                    EditorUtil.hideToolBar(toolbarLayout)
                }
                "zss_field_content" -> {
                    EditorUtil.showToolBar(toolbarLayout)
                }
            }
        }
    }

    override fun onMediaTapped(mediaId: String?, mediaType: MediaType?, meta: JSONObject?, uploadStatus: String?) {

    }

    override fun onMediaReplaced(mediaId: String?) {

    }

    override fun onActionFinished() {

    }

    override fun onSelectionStyleChanged(changeMap: MutableMap<String, Boolean>) {
        for ((key, value) in changeMap.entries) {
      //      toggleMap[key]?.setCheck(value)
        }
    }

    override fun onVideoPressInfoRequested(videoId: String?) {

    }


    override fun onDomLoaded() {
//        aiwoEditor?.load()
//        runOnUiThread {
//            editorState = EditorState.SELECT
//            aiwoEditor?.getAllHTML()
//        }
    }

    override fun onMediaRemoved(mediaId: String?) {

    }


//    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
//        super.onActivityResult(requestCode, resultCode, data)
//        if (requestCode == ChooseTopicActivity.TOPIC_ID_CODE && resultCode == ChooseTopicActivity.TOPIC_ID_CODE && data != null) {
//            handleTopic(
//                data.getParcelableExtra<EditorTopicBean>("topic")?.id,
//                data.getParcelableExtra<EditorTopicBean>("topic")?.content,
//                data.getBooleanExtra("removeTopic", false)
//            )
//        }
//    }

    override fun onDestroy() {
      //  aiwoEditor?.destory()
        super.onDestroy()
    }
}